package com.xilihui.xlh.wxapi;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.xilihui.xlh.R;
import com.xilihui.xlh.business.activitys.ConfirmListingActivity;
import com.xilihui.xlh.business.activitys.MyChecklistActivity;
import com.xilihui.xlh.business.activitys.store.ConfirmOrderActivity;
import com.xilihui.xlh.business.activitys.store.StoreOrderActivity;
import com.xilihui.xlh.business.activitys.store.TradeSuccessActivity;
import com.xilihui.xlh.constant.StrConst;
import com.xilihui.xlh.core.util.LogUtil;
import com.xilihui.xlh.core.util.SPUtil;
import com.xilihui.xlh.core.util.ToastUtil;
import com.xilihui.xlh.core.util.YEventBuses;

import org.greenrobot.eventbus.Subscribe;


public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
	
    private IWXAPI api;
    
    String type = "";
    String wa_id = "";
    String agent_id = "";
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_pay_result);
		
    	api = WXAPIFactory.createWXAPI(this, StrConst.WEIXIN_ID);
        api.handleIntent(getIntent(), this);
        
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}
	
	@Override
	public void onReq(BaseReq req) {
		
    }
	
	@Override
	public void onResp(BaseResp resp) {
		LogUtil.i("mylog", "onPayFinish, errCode = " + resp.errCode);
		
		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			int code = resp.errCode;
			if(code==0){
				ToastUtil.toastShortPositive("支付成功");
			}else {
				ToastUtil.toastShortPositive("支付失败");
			}
			
			
			boolean flag = (boolean) SPUtil.get(WXPayEntryActivity.this,SPUtil.TIANZHUANG,false);
			wa_id = (String)SPUtil.get(WXPayEntryActivity.this,SPUtil.WA_ID,"");
			agent_id = (String)SPUtil.get(WXPayEntryActivity.this,SPUtil.ARGENT_ID,"");
			
			int type = (int)SPUtil.get(WXPayEntryActivity.this,SPUtil.PAY_TYPE,1);
			//type = 1 仓库领货 2（商城）
			if(type==1){
				if(!flag){
					if(code==0){
						YEventBuses.post(new YEventBuses.Event("yifukuang"));
					}
				}else {
					boolean isTiaozhuang = false;//是否是 确认清单的跳转
					SPUtil.put(WXPayEntryActivity.this,SPUtil.TIANZHUANG,isTiaozhuang);
					Intent intent = new Intent(WXPayEntryActivity.this,MyChecklistActivity.class);
					intent.putExtra("wa_id",wa_id);
					intent.putExtra("agent_id",agent_id);
					startActivity(intent);
					YEventBuses.post(new YEventBuses.Event("pay"));
				}
			}else if(type==2){
				boolean tzflag = (boolean)SPUtil.get(WXPayEntryActivity.this,SPUtil.STORE_TZ,false);
				if(!tzflag){
					if(code==0){
						YEventBuses.post(new YEventBuses.Event("yifukuang"));
					}
				}else {
					boolean isTiaoZhuang = false;//是否是 跳转我的订单
					SPUtil.put(WXPayEntryActivity.this,SPUtil.STORE_TZ,isTiaoZhuang);
					if(code==0){
						String jiner = (String)SPUtil.get(WXPayEntryActivity.this,"jiner","0");
						String jifen = (String)SPUtil.get(WXPayEntryActivity.this,"jifen","0");
						Intent intent = new Intent(WXPayEntryActivity.this, TradeSuccessActivity.class);
						intent.putExtra("jiner",jiner);
						intent.putExtra("jifen",jifen);
						intent.putExtra("type",2);
						startActivity(intent);
					}else {
						Intent intent = new Intent(WXPayEntryActivity.this, StoreOrderActivity.class);
						startActivity(intent);
					}
					
					YEventBuses.post(new YEventBuses.Event("pay"));
				}
				
			}
			
			
		}
		finish();
	}
	
	
	
}